
import {
  queryPromotionPeople,
  queryPromotionPeopleList
} from "../../../service/jb_promotion_service"
import {
  queryActivityList
} from "../../../service/jb_activity_service"
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    menuTop: app.globalData.menuButton.top,
    menuHeight: app.globalData.menuButton.height,
    topSwiperIndex: 0,
    swiperType: 'week',

    page: 1,
    limit: 20,
    status: 1,
    dataList: [


    ],
    isShowLoading: false,
    isBottomShow: false,
    totalCount: 0,
    refreshFlag: false,
    totalLeftNum: 0,
    totalRightNum: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // this.queryPromotionPeople();
    this.queryPromotionPeopleList('refresh');
  },


  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  pushToWorkList(e) {
    let item = e.currentTarget.dataset.item;
    app.globalData.voteEndTime = item.endTime;
    app.globalData.voteData = item;
    wx.navigateTo({
      url: '/featurePackage/pages/worksList/worksList?voteId=' + item.id,
    })
  },
  topSwiperChange(e) {
    let index = e.currentTarget.dataset.index;
    this.setData({
      topSwiperIndex: index,
      page: 1,
      status: index == 0 ? 1 : 2,
      isBottomShow: false,
    })
    this.queryPromotionPeopleList('refresh')
  },

  searchInputChange(e) {
    this.setData({
      "requestData.keyword": e.detail.value,
    })
  },
  //开始搜索
  handleSearchAction() {
    console.log("JJLog:开始搜索了：" + this.data.requestData.keyword);
    this.setData({
      isBottomShow: false,
      'requestData.page': 1
    })
    this.queryPromotionPeopleList('refresh');
  },

  queryPromotionPeople() {
    wx.showLoading();
    queryPromotionPeople().then(res => {
      wx.hideLoading();
      if (res.code == 200) {
        this.setData({
          totalCount: res.data.total || 0
        })
      }
    }).catch(err => {
      wx.hideLoading();
      wx.showToast({
        title: err || '加载失败',
        icon: "none"
      })
      console.error('JJLog:queryPromotionPeople', err);
    })
  },
  queryPromotionPeopleList(type) {
    wx.showLoading();
    queryActivityList(this.data.status, this.data.page, this.data.limit).then(res => {
      wx.hideLoading();
      if (res.code == 200 && res.data) {
        let arr = (type == 'refresh' ? [] : this.data.dataList || []);
        arr = arr.concat(res.data.list || [])
        this.setData({
          dataList: arr,
          refreshFlag: false,
          isShowLoading: false,
          isBottomShow: res.data.totalPage == this.data.page ? true : false,
        })

      }
    }).catch(err => {
      wx.hideLoading();
      wx.showToast({
        title: err || '加载失败',
        icon: "none"
      })
      if (this.data.page != 1) {
        this.setData({
          page: this.data.page - 1
        })
      }
      this.setData({
        refreshFlag: false,
      })
      console.error('JJLog:queryPromotionPeopleList', err);
    })
  },

  //下拉刷新
  handleRefresh() {
    // TODO
    this.setData({
      page: 1,
      isBottomShow: false,
      refreshFlag: true,
    })
    this.queryPromotionPeopleList('refresh');
  },
  handleLoadmore() {
    console.log('加载更多');
    if (this.data.isBottomShow) {
      return;
    }
    this.setData({
      isShowLoading: true,
      page: this.data.page + 1
    })
    this.queryPromotionPeopleList()
  },


})